package com.twb.auth.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.auth.entity.twbApi.TwbApi;
import com.twb.auth.entity.twbApi.dto.GetTwbApiByIdDto;
import com.twb.auth.entity.twbApi.dto.SearchTwbApiDto;
import com.twb.auth.entity.twbApi.vo.SaveTwbApiVo;
import com.twb.auth.entity.twbApi.vo.SearchTwbApiVo;
import com.twb.auth.entity.twbApi.vo.UpdateTwbApiByIdVo;
import com.twb.auth.service.ITwbApiService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Title  TwbApiController
 * @Description API信息

 * @author DSY
 * @date 2021-05-13 13:43:43
 */
@RestController
@RequestMapping("/auth/twbApi")
@Api(tags = "API信息",value="TwbApiController")
public class TwbApiController extends BaseController{
	@Autowired
	ITwbApiService twbApiService;

	/**
	 * @Title  search
	 * @Description 分页查询
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
	@GetMapping("/search")
	@ApiOperation(value="分页查询")
	public Result<TwbPageInfo<SearchTwbApiDto>> search(SearchTwbApiVo vo){
		TwbPageInfo<SearchTwbApiDto> result = twbApiService.searchTwbApi(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title searchList
	 * @Description 不分页查询
	 * @param vo
	 * @return com.twb.core.base.Result<java.util.List<com.twb.auth.entity.twbApi.dto.SearchTwbApiDto>>
	 * @author DHL
	 * @date 13:59 2022/3/23
	 */
	@GetMapping("/searchList")
	@ApiOperation(value="不分页查询")
	public Result<List<SearchTwbApiDto>> searchList(SearchTwbApiVo vo){
		List<SearchTwbApiDto> result = twbApiService.searchTwbApiList(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title  getById
	 * @Description 根据ID获取详情
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
	@GetMapping("/getById")
	@ApiOperation(value="根据ID获取详情")
	public Result<GetTwbApiByIdDto> getById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		TwbApi twbApi = twbApiService.getById(id);
		if(twbApi == null) {
			return Results.failure("数据不存在");
		}
		GetTwbApiByIdDto dto = new GetTwbApiByIdDto();
		BeanUtils.copyNotNullProperties(twbApi, dto);
		return Results.successWithData(dto);
	}

	/**
	 * @Title  save
	 * @Description 保存信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
	@PostMapping("/save")
	@ApiOperation(value="保存信息")
	public Result<String> save(@Valid SaveTwbApiVo vo){
		boolean result = twbApiService.saveTwbApi(vo);
		if(result) {
			return Results.success("保存成功");
		}else {
			return Results.failure("保存失败");
		}
	}

	/**
	 * @Title  updateById
	 * @Description 更新信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
	@PutMapping("/updateById")
	@ApiOperation(value="更新信息")
	public Result<String> updateById(@RequestBody @Valid UpdateTwbApiByIdVo vo){
		boolean result = twbApiService.updateTwbApiById(vo);
		if(result) {
			return Results.success("更新成功");
		}else {
			return Results.failure("更新失败");
		}
	}


	/**
	 * @Title  deleteById
	 * @Description 删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
	@PostMapping("/deleteById")
	@ApiOperation(value="删除信息")
	public Result<String> deleteById(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String id){
		boolean result = twbApiService.deleteTwbApiById(id);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}


	/**
	 * @Title  deleteBatchByIds
	 * @Description 批量删除信息
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
	@PostMapping("/batchDeleteByIds")
	@ApiOperation(value="批量删除信息")
	public Result<String> deleteBatchByIds(
			@NotBlank
			@RequestParam
			@ApiParam(value="主键",name="id",required=true)
			String ids){
		boolean result = twbApiService.deleteTwbApiByIds(ids);
		if(result) {
			return Results.success("删除成功");
		}else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title listApiIdsByResourceId
	 * @Description 获取资源API ID
	 * @param resourceId
	 * @return
	 * @author DSY
	 * @date 2021年6月29日 下午12:08:46
	 */
	@Auth(verifyURL = false)
	@GetMapping("/listApiIdsByResourceId")
	@ApiOperation(value = "获取资源API ID")
	public Result<List<SearchTwbApiDto>> listApiIdsByResourceId(@NotBlank @RequestParam String resourceId) {
		List<SearchTwbApiDto> apiIds = twbApiService.listApiIdsByResourceId(resourceId);

		return Results.successWithData(apiIds);
	}

	/**
	 * @Title  logicDeleteById
	 * @Description 逻辑删除信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
//	@Auth(verifyToken = true)
//	@PostMapping("/logicDeleteById")
//	@ApiOperation(value = "逻辑删除信息")
//	public Result<String> logicDeleteById(
//			@NotBlank
//			@RequestParam
//			String id){
//		TwbApi record = twbApiService.get(id);
//		if(null == record) {
//			return Results.failure("信息不存在");
//		}
//		record = new TwbApi();
//		record.setId(id);
//		twbApiService.setUpdateInfo(record);
//		record.setIsdel(1);
//		int result = twbApiService.updateSelective(record);
//		if(result>0) {
//			return Results.success("删除成功");
//		}else {
//			return Results.failure("删除失败");
//		}
//	}


	/**
	 * @Title  logicBatchDeleteByIds
	 * @Description 批量逻辑删除
	 * @param ids
	 * @return
	 * @author DSY
	 * @date 2021-05-13 13:43:43
	 */
//	@Auth(verifyToken = true)
//	@PostMapping("/logicBatchDeleteByIds")
//	@ApiOperation(value = "逻辑删除信息")
//	public Result<String> logicBatchDeleteByIds(
//			@NotBlank
//			@RequestParam
//			String ids){
//		int result = twbApiService.logicBatchDeleteTwbApiByIds(ids);
//		if(result>0) {
//			return Results.success("删除成功");
//		}else {
//			return Results.failure("删除失败");
//		}
//	}


	/**
	 * @Title  exportExcel
	 * @Description 导出excel
	 * @param exportExcelVo
	 * @param searchVo
	 * @param request
	 * @return
	 * @author DSY
	 * @throws Exception 
	 * @date 2021-05-13 13:43:43
	 */
	@Auth(verifyURL=false)
	@PostMapping("/exportExcel")
	@ApiOperation(value="导出excel")
	public void exportExcel(@RequestBody SearchTwbApiVo vo, HttpServletResponse response) throws Exception{
		twbApiService.exportExcel(vo, response);
	}
}